package cn.jh.common.pay.utils;

import com.alibaba.fastjson.JSON;
import com.google.common.base.MoreObjects;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

/**
 *
 * @author Yichuan
 * @version 1.0
 */
public class PayBarRequest implements Serializable {

    private static final long serialVersionUID = -3383152910742979437L;

    /** 是否支持签名,默认为不支持 */
    private boolean supportSign = false;

    /** 加密算法类型: AES/3DES */
    private EncryptType encryptType;

    /** 商户秘钥,用于签名及加密 */
    private String secretKey;

    /** 商户编号 */
    private String merchantNo;

    /** 请求参数, 按字典顺序排序 */
    private Map<String, String> params = new TreeMap<>();

    public PayBarRequest() {}

    public PayBarRequest(String merchantNo, String secretKey) {
        this.merchantNo = merchantNo;
        this.secretKey = secretKey;
    }

    public void addParam(String key, String value) {
        if (StringUtils.isBlank(key)) {
            throw new IllegalArgumentException("param key不能为空");
        }
        this.params.put(key.trim(), (value==null) ? "" : value.trim());
    }

    public void addParams(Map<String, String> paramMap) {
        if (!paramMap.isEmpty()) {
            for (Map.Entry<String, String> entry: paramMap.entrySet()) {
                this.params.put(entry.getKey().trim(),
                        entry.getValue() == null ? "" : entry.getValue().trim());
            }
        }
    }

    public boolean isSupportSign() {
        return supportSign;
    }

    public void setSupportSign(boolean supportSign) {
        this.supportSign = supportSign;
    }

    public EncryptType getEncryptType() {
        return encryptType;
    }

    public void setEncryptType(EncryptType encryptType) {
        this.encryptType = encryptType;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getMerchantNo() {
        return merchantNo;
    }

    public void setMerchantNo(String merchantNo) {
        this.merchantNo = merchantNo;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("supportSign", supportSign)
                .add("encryptType", encryptType.name())
                .add("secretKey", secretKey)
                .add("merchantNo", merchantNo)
                .add("paramMap", JSON.toJSONString(params)).toString();
    }
}
